package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: itheima
 * @create: 2022-01-12 11:56
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {


    //至少在当前业务逻辑层定义方法：有当前模块基本CRUD方法

   /* @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public WmChannel getById(Integer id) {
        return wmChannelMapper.selectById(id);
    }*/


    /**
     * 频道分页条件查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByName(ChannelDto dto) {
        //1.参数校验构建条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            //1.1设置模糊查询频道名称
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        //2.执行分页条件查询
        IPage<WmChannel> page = this.page(new Page<>(dto.getPage(), dto.getSize()), queryWrapper);
        //3.处理分页结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    @Override
    public ResponseResult saveChannel(WmChannel entity) {
        // 需求分析
        // 参数校验
        if (entity == null || StringUtils.isEmpty(entity.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 查询当前频道名称是否已经存在,已经存在,提示不能新增
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName, entity.getName());
        WmChannel one = this.getOne(query);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 保存频道
        this.save(entity);
        return ResponseResult.okResult();
    }


    @Override
    public ResponseResult deleteChannel(Integer id) {
        // 根据id查询频道
        WmChannel channel = this.getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 只有禁用的频道才能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_DELETE);
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }
}
